﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Runtime;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Xml;

namespace SCOSub
{
    public partial class Setting : Form
    {
        
        public delegate void SetAutoSaveTimerEventHandle();
        public event SetAutoSaveTimerEventHandle SetAutoSaveEvent;//设置自动保存
        public delegate void SetMainFormTopMostHandle(bool topmost);
        public event SetMainFormTopMostHandle SetMainFormTopMost;//设置MainForm的TopMost

        public Setting()
        {
            InitializeComponent();
            this.TopMost = Const.DoTopMost;

            MemAddress_textbox.Text = Const.Address.ToString();
            TooShort_numericUpDown1.Value = Const.MinLen;
            TooLong_numericUpDown.Value = Const.MaxLen;
            AutoSaveSpan_numericUpDown.Value = Const.AutoSaveSpan;
            MinLastTimenumericUpDown1.Value = Const.MinLastTime;
            DoTopMost_checkBox.Checked = Const.DoTopMost;
            ffms2framebuffernumericUpDown.Value = Const.FFMS2VideoProviderFrameBuffer;
            hotkey_comboBox.SelectedIndex = Const.HotKeyID - 112;
            AutoOffset_comboBox.SelectedIndex = (int)Const.autooffsetmode - 1;
        }
        //
        //编辑头文件
        //
        private void OpenFile(string filename, string DefaultHead)
        {
            SetMainFormTopMost(false);
            this.TopMost = false;
            FileInfo fi = new FileInfo(Application.StartupPath + "\\" + filename);
            if (fi.Exists)
            {
                Process.Start("notepad.exe", fi.FullName);

            }
            else
            {
                StreamWriter sw = new StreamWriter(fi.FullName, false, Encoding.Unicode);
                StringReader sr = new StringReader(DefaultHead);
                while (sr.Peek() > -1)
                {
                    sw.WriteLine(sr.ReadLine());
                }
                sr.Close();
                sw.Close();
                Process.Start("notepad.exe", fi.FullName);

            }

        }
        private void StaffEditButton_Click(object sender, EventArgs e)
        {
            OpenFile("staff.h", ChangeFormat_Staff_Mode.StaffDefaultHead);
        }
        private void TimeEditbutton_Click(object sender, EventArgs e)
        {
            OpenFile("time.h", ChangeFormat_Time_Mode.TimeDefaultHead);
        }
        private void EffectEditbutton_Click(object sender, EventArgs e)
        {
            OpenFile("effect.h", ChangeFormatEffects.EffectDefaultHead);
        }
        //
        //搜索内存
        //
        private void ScanAddress_button_Click(object sender, EventArgs e)
        {
            this.Text = "设置  搜索中...";
            ScanMemAddress sc = new ScanMemAddress();

            int address = sc.scan();
            if (address == 0)
                MessageBox.Show("搜索失败");
            else if (address == -1)
                MessageBox.Show("没找到窗口");
            else
                MessageBox.Show("搜索完成");
            MemAddress_textbox.Text = address.ToString();
            this.Text = "设置";
        }
        //
        //保存退出
        //
        private void OK_button_Click(object sender, EventArgs e)
        {
            int addr = 0;
            try
            {
                addr = int.Parse(MemAddress_textbox.Text);
            }
            catch
            {
                MessageBox.Show("地址错误");
                return;
            }
            if (TooLong_numericUpDown.Value < TooShort_numericUpDown1.Value)
            {
                MessageBox.Show("时间轴：长度限定错误");
                return;
            }
            {
                Const.Address = addr;
                Const.MaxLen = (int)TooLong_numericUpDown.Value;
                Const.MinLen = (int)TooShort_numericUpDown1.Value;
                Const.AutoSaveSpan = (int)AutoSaveSpan_numericUpDown.Value;
                Const.MinLastTime = (int)MinLastTimenumericUpDown1.Value;
                Const.DoTopMost = DoTopMost_checkBox.Checked;
                Const.FFMS2VideoProviderFrameBuffer = (int)ffms2framebuffernumericUpDown.Value;
                Const.HotKeyID = hotkey_comboBox.SelectedIndex + 112;
                Const.autooffsetmode = (AutoOffsetMode)(AutoOffset_comboBox.SelectedIndex + 1);
            }
            Const.SaveConfig();
            

            SetAutoSaveEvent();//触发 设置自动保存闹钟 事件
            this.Close();
        }
       
        private void Cancel_button_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Setting_FormClosing(object sender, FormClosingEventArgs e)
        {
            SetMainFormTopMost(true);
        }

    }
}